package io.renren.wh.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import io.renren.wh.merchant.dao.InfoDao;
import io.renren.wh.merchant.dao.MerchantKeyDao;
import io.renren.wh.merchant.entity.InfoEntity;
import io.renren.wh.merchant.entity.InfoFaceRelation;
import io.renren.wh.merchant.entity.MerchantKeyEntity;
import io.renren.wh.merchant.service.InfoService;
import io.renren.wh.merchant.util.AppUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("infoService")
public class InfoServiceImpl extends ServiceImpl<InfoDao, InfoEntity> implements InfoService {
    @Resource
    private MerchantKeyDao merchantKeyDao;
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private SysUserService sysUserService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //1、通过key获取前端数据

        String merchantName = (String) params.get("merchantName");  //商户名称
        String clearing = (String) params.get("clearing");  //结算方式
        String status = (String) params.get("status1"); //商户状态
        QueryWrapper<InfoEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(merchantName)) {
            queryWrapper.eq("merchantName", merchantName);
        }
        if (!StringUtils.isEmpty(clearing )&& !clearing.equals("3") ) {
            queryWrapper.eq("clearing", clearing);
        }
        if (!StringUtils.isEmpty(status) && !status.equals("3")) {
            queryWrapper.eq("status", status);
        }

        IPage<InfoEntity> page = this.page(
                new Query<InfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void insertFaceId(InfoEntity info) {

        if (info != null) {


        }
    }

    @Override
    public void insertInterfacesRelation(List<InfoFaceRelation> vo) {

        baseMapper.insertInterfacesRelation(vo);
    }

    @Transactional
    @Override
    public void saveAndCreateAccount(InfoEntity info) {
        super.save(info);
        //自动创建关联账号
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setUsername(info.getAccountnumber());
        sysUserEntity.setPassword("88888888");
        sysUserEntity.setStatus(1);
        sysUserEntity.setCreateTime(new Date());
        sysUserEntity.setMobile(info.getContactsphone());
        sysUserEntity.setCreateUserId(1L);

        List<Long> roleList = new ArrayList<>();
        roleList.add(2L); //商户角色ID为2
        sysUserEntity.setRoleIdList(roleList);

        sysUserService.saveUser(sysUserEntity);

//        sysUserService.save(sysUserEntity);
//
//        sysUserRoleService.saveOrUpdate(sysUserEntity.getUserId(), sysUserEntity.getRoleIdList());

        // 将 APPID 秘钥 插入到数据库表
        String appId = AppUtils.getAppId();
        String secretKey = AppUtils.getAppSecret(appId,info.getAccountnumber());

        MerchantKeyEntity merchantKeyEntity = new MerchantKeyEntity();
        merchantKeyEntity.setAppid(appId);
        merchantKeyEntity.setSecretkey(secretKey);
        merchantKeyEntity.setMerchantid(info.getId());
        merchantKeyEntity.setMerchantaccount(info.getAccountnumber());

        merchantKeyDao.insert(merchantKeyEntity);
    }
}
